/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.util.ArrayList;
import java.util.Date;
import sun.applet.resources.MsgAppletViewer;
import util.Message.MessageType;

/**
 *
 * @author Jonas
 */
public class SnifferDummy implements Sniffer {
    private String outputText;
    private ArrayList<SnifferListener> subscribers = new ArrayList<SnifferListener>();

    public SnifferDummy() {
        outputText="Sniffer started @ "+new Date(System.currentTimeMillis());
    }

    public void readTransmission(Message message,NetwerkComponent from, NetwerkComponent to) {
        String newEntry = "";
        if(from.toString().equals(to.toString())){
            newEntry += "<font color=#aaaaaa><i>";
        }
        newEntry += "<b>@ "+new Date(System.currentTimeMillis())+"</b><br />";
        newEntry += "<b>TYPE ---&gt; "+message.getMessageType()+"</b>\n";
        newEntry += "<dl><dt><b>From:</b></dt><dd>"+from+"</dd>";
        newEntry += "<dt><b>To:</b></dt><dd>"+to+"</dd>";
        newEntry += "<dt><b>Message:</b></dt>";


        if(message.getMessageType() == MessageType.TS_REQ){
            newEntry += "<dd>"+"<i><u>Signature:</u></i> "+new String((byte[])message.getData().get("Signature"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.TS_ANSWER)
        {
            newEntry += "<dd>"+"<i><u>Signature:</u></i> "+new String((byte[])message.getData().get("Signature"))+"</dd>";
            newEntry += "<dd>"+"<i><u>Timestamp:</u></i> "+new Date((Long)message.getData().get("Timestamp"))+"</dd>";
            newEntry += "<dd>"+"<i><u>SignTS:</u></i> "+new String((byte[])message.getData().get("SignTS"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.CLIENT_HELLO)
        {
            newEntry += "<dd>"+"<i><u>Client_nonce:</u></i> "+(String)(message.getData().get("client_nonce"))+"</dd>";
            newEntry += "<dd>"+"<i><u>RSA:</u></i> "+(String)(message.getData().get("RSA"))+"</dd>";
            newEntry += "<dd>"+"<i><u>SHA-256:</u></i> "+(String)(message.getData().get("SHA-256"))+"</dd>";
            newEntry += "<dd>"+"<i><u>AES:</u></i> "+(String)(message.getData().get("AES"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.SERVER_HELLO)
        {
            newEntry += "<dd>"+"<i><u>Response:</u></i> "+(String)(message.getData().get("response"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.SERVER_CERT)
        {
            newEntry += "<dd>"+"<i><u>Certificate:</u></i> "+(Certificate)(message.getData().get("certificate"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.CLIENT_CERT)
        {
            newEntry += "<dd>"+"<i><u>Certificate:</u></i> "+(Certificate)(message.getData().get("certificate"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.CLIENT_KEY_EXCHANGE)
        {
            newEntry += "<dd>"+"<i><u>Salt:</u></i> "+new String((byte[])(message.getData().get("salt")))+"</dd>";
            newEntry += "<dd>"+"<i><u>Pre_master_secret:</u></i> "+new String((char[])message.getData().get("pre_master_secret"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.CONN_OK)
        {
            
        }
        else if(message.getMessageType() == MessageType.RRS_REQ)
        {
            int[] cats = (int[])message.getData().get("FCat");
            newEntry += "<dd>"+"<i><u>TBegin:</u></i> "+new Date((Long)message.getData().get("TBegin"))+"</dd>";
            newEntry += "<dd>"+"<i><u>TEnd:</u></i> "+new Date((Long)message.getData().get("TEnd"))+"</dd>";
            newEntry += "<dd>"+"<i><u>TCat:</u></i> DUUR = "+cats[0]+" - STANDAARD = "+cats[1]+" - GOEDKOOP = "+cats[2]+"</dd>";
            newEntry += "<dd>"+"<i><u>Signature:</u></i> "+new String((byte[])message.getData().get("Signature"))+"</dd>";
            newEntry += "<dd>"+"<i><u>Timestamp:</u></i> "+new Date((Long)message.getData().get("Time"))+"</dd>";
            newEntry += "<dd>"+"<i><u>SignTS:</u></i> "+new String((byte[])message.getData().get("SignTS"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.RRS_ANSWER)
        {
            newEntry += "<dd>"+"<i><u>TBegin:</u></i> "+new Date((Long)message.getData().get("TBegin"))+"</dd>";
            newEntry += "<dd>"+"<i><u>TEnd:</u></i> "+new Date((Long)message.getData().get("TEnd"))+"</dd>";
            newEntry += "<dd>"+"<i><u>Signature:</u></i> "+new String((byte[])message.getData().get("Signature"))+"</dd>";
            newEntry += "<dd>"+"<i><u>Timestamp:</u></i> "+new Date((Long)message.getData().get("Time"))+"</dd>";
            newEntry += "<dd>"+"<i><u>SignTS:</u></i> "+new String((byte[])message.getData().get("SignTS"))+"</dd>";
        }
        else if(message.getMessageType() == MessageType.PRE_ENCRYPTED)
        {
            ArrayList<byte[]> encryption = (ArrayList<byte[]>)message.getData().get("encrypted");
            for(int i=0;i<encryption.size();i++){
                String part = (encryption.size() > 1)?" (part "+i+")":"";
                newEntry += "<dd>"+"<i><u>Encrypted"+part+":</u></i> "+new String(encryption.get(i))+"</dd>";
            }
        }
        else if(message.getMessageType() == MessageType.ENCRYPTED)
        {
            newEntry += "<dd>"+"<i><u>Encrypted:</u></i> "+(String)(message.getData().get("encrypted"))+"</dd>";
        }
        else{
            for(String key : message.getData().keySet())
                newEntry += "<dd><i><u>"+key+":</u></i> "+message.getData().get(key)+"</dd>";
        }
        if(from.toString().equals(to.toString())){
            newEntry += "</i></font>";
        }
        newEntry += "</dl><hr />";
        outputText = newEntry + outputText;

        for(SnifferListener subscriber:subscribers){
            subscriber.outputChanged(outputText);
        }
    }

    public void addListener(SnifferListener subscriber) {
        subscribers.add(subscriber);
        subscriber.outputChanged(outputText);
    }

    public void removeListener(SnifferListener subscriber) {
        if(subscribers.contains(subscriber)){
            subscribers.remove(subscriber);
        }
    }
}
